<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- <el-checkbox label="0">男</el-checkbox> -->
      <el-checkbox
        v-for="role in list"
        :key="role.id"
        :label="role.id"
      >{{ role.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
import { getRoles } from '@/api/settings'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 当前选中的角色的id
      list: [] // 所有的角色
    }
  },
  // created() {
  //   // 组件创建时执行一次
  //   this.loadRoles()
  // },

  methods: {
    async loadRoles() {
      console.log('子组件', this.employeeId)
      const res = await getRoles({ page: 1, pagesize: 100 })

      const info = await getUserDetailById(this.employeeId)

      // 保存所有的角色
      this.list = res.data.rows
      // console.log('loadRoles...........', res)
      console.log('getUserDetailById...........', info)
      // 保存这个员工当前的已经有的角色
      this.roleIds = info.data.roleIds
    },
    async hSubmit() {
      const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      console.log('保存角色', res)
      this.$emit('update-close')
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
